package io.renren.controller;

import io.renren.entity.DfspEntity;
import io.renren.entity.SysUserEntity;
import io.renren.entity.VSalaryBaseicinfoEntity;
import io.renren.entity.VSocialsbSummaryEntity;
import io.renren.service.DfspService;
import io.renren.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 
 * 
 * @author zhangheng
 * @email zh_123shmily@163.com
 * @date 2017-07-28 21:06:59
 */
@RestController
@RequestMapping("dfsp")
public class DfspController extends  AbstractController {
	@Autowired
	private DfspService dfspService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("dfsp:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        QueryAuthority query = new QueryAuthority(params);

		List<DfspEntity> dfspList = dfspService.queryList(query);
		int total = dfspService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(dfspList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	@RequestMapping("/salarylist")
	public R salaryList(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);

		List<VSalaryBaseicinfoEntity> dfspList = dfspService.querySalaryList(query);
		int total = dfspService.querySalaryTotal(query);

		PageUtils pageUtil = new PageUtils(dfspList, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}
	@RequestMapping("/socialsblist")
	public R socialsbList(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);

		List<VSocialsbSummaryEntity> dfspList = dfspService.querySocialsbList(query);
		int total = dfspService.querySocialsbTotal(query);

		PageUtils pageUtil = new PageUtils(dfspList, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{dfId}")
	@RequiresPermissions("dfsp:info")
	public R info(@PathVariable("dfId") Integer dfId){
		DfspEntity dfsp = dfspService.queryObject(dfId);
		
		return R.ok().put("dfsp", dfsp);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("dfsp:save")
	public R save(@RequestBody DfspEntity dfsp){
		SysUserEntity user = (SysUserEntity) ShiroUtils.getSubject().getSession().getAttribute("user");

		dfsp.setCompanyId(user.getCompanyId());
		dfsp.setCusername(user.getUsername());
		dfsp.setDeptId(user.getDeptNo());
		dfsp.setCompany(user.getCompany());
		dfsp.setDept(user.getDept());
		dfsp.setPost(user.getPost());

		dfsp.setUserdesc(user.getUserdesc());

		dfspService.save(dfsp);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("dfsp:update")
	public R update(@RequestBody DfspEntity dfsp){
		dfspService.update(dfsp);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("dfsp:delete")
	public R delete(@RequestBody Integer[] dfIds){
		dfspService.deleteBatch(dfIds);
		
		return R.ok();
	}


	/**
	 * 提交
	 */
	@RequestMapping("/submit")
	@RequiresPermissions("dfsp:submit")
	public R submit(@RequestBody Integer id){
		DfspEntity entity = dfspService.queryObject(id);
		String msg=dfspService.submit(entity);
		if(StringUtils.isNotBlank(msg)){
			return R.error(1, msg);
		}
		return R.ok();
	}


	/**
	 * 批准
	 */
	@RequestMapping("/process")
	public R process(@RequestBody DfspEntity entity){

		String loginer=getUser().getUsername()==null?"":getUser().getUsername();
		if(!loginer.equals(entity.getNextUser())){
			return R.error(2,"当前登录人非流程下个审批人，不能审批");
		}

		dfspService.process(entity);

		return R.ok();
	}

	/**
	 * 拒绝
	 */
	@RequestMapping("/reject")
	public R reject(@RequestBody DfspEntity entity){

		String loginer=getUser().getUsername()==null?"":getUser().getUsername();
		if(!loginer.equals(entity.getNextUser())){
			return R.error(2,"当前登录人非流程下个审批人，不能审批");

		}
		dfspService.reject(entity);

		return R.ok();
	}
	
}
